{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "###################################  模型训练  ###################################\n",
    "\n",
    "import numpy as np # 矩阵操作\n",
    "import pandas as pd # SQL数据处理\n",
    "from sklearn.metrics import mean_squared_error  # 模型性能评价\n",
    "\n",
    "import matplotlib.pyplot as plt   #画图\n",
    "\n",
    "# 图形出现在Notebook里而不是新窗口\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.355170</td>\n",
       "      <td>0.373517</td>\n",
       "      <td>0.828620</td>\n",
       "      <td>0.284606</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.379232</td>\n",
       "      <td>0.360541</td>\n",
       "      <td>0.715771</td>\n",
       "      <td>0.466215</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.171000</td>\n",
       "      <td>0.144830</td>\n",
       "      <td>0.449638</td>\n",
       "      <td>0.465740</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.175530</td>\n",
       "      <td>0.174649</td>\n",
       "      <td>0.607131</td>\n",
       "      <td>0.284297</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.209120</td>\n",
       "      <td>0.197158</td>\n",
       "      <td>0.449313</td>\n",
       "      <td>0.339143</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 35 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant  season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  \\\n",
       "0        1         1         0         0         0       1       0       0   \n",
       "1        2         1         0         0         0       1       0       0   \n",
       "2        3         1         0         0         0       1       0       0   \n",
       "3        4         1         0         0         0       1       0       0   \n",
       "4        5         1         0         0         0       1       0       0   \n",
       "\n",
       "   mnth_4  mnth_5  ...  weekday_5  weekday_6      temp     atemp       hum  \\\n",
       "0       0       0  ...          0          1  0.355170  0.373517  0.828620   \n",
       "1       0       0  ...          0          0  0.379232  0.360541  0.715771   \n",
       "2       0       0  ...          0          0  0.171000  0.144830  0.449638   \n",
       "3       0       0  ...          0          0  0.175530  0.174649  0.607131   \n",
       "4       0       0  ...          0          0  0.209120  0.197158  0.449313   \n",
       "\n",
       "   windspeed  holiday  workingday  yr   cnt  \n",
       "0   0.284606        0           0   0   985  \n",
       "1   0.466215        0           0   0   801  \n",
       "2   0.465740        0           1   0  1349  \n",
       "3   0.284297        0           1   0  1562  \n",
       "4   0.339143        0           1   0  1600  \n",
       "\n",
       "[5 rows x 35 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预处理数据导入\n",
    "train_datas = pd.read_csv(r'E:/188-163-2-1555990564/FE_day.csv')\n",
    "train_datas.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从数据中分离输入特征x和输出y\n",
    "y = pd.DataFrame(train_datas,columns = ['cnt'])\n",
    "X = train_datas.drop([\"cnt\"], axis = 1)\n",
    "\n",
    "# 特征名称，后续显示权重系数对应的特征\n",
    "feature_names = X.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(584, 34)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入数据分割工具包\n",
    "from sklearn.model_selection import train_test_split\n",
    "# 对全体数据，随机选择其中80%做训练数据，剩下20%为测试数据\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.2)\n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "###################  使用最小二乘线性回归模型  ###################\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "# 使用缺省的线性回归实例\n",
    "lr = LinearRegression()\n",
    "# 使用训练数据训练模型参数\n",
    "lr.fit(X_train, y_train)\n",
    "# 使用训练好的模型对测试集进行预测\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The RMSE score of LinearRegression on test is 814.4749076863649\n",
      "The RMSE score of LinearRegression on train is 742.7543512758713\n"
     ]
    }
   ],
   "source": [
    "# 使用【RMSE】评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "# 测试集\n",
    "print('The RMSE score of LinearRegression on test is', np.sqrt(mean_squared_error(y_test, y_test_pred_lr)))\n",
    "# 训练集\n",
    "print('The RMSE score of LinearRegression on train is',np.sqrt(mean_squared_error(y_train, y_train_pred_lr)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The RMSE score of RidgeCV on test is 812.0412685340641\n",
      "The RMSE score of RidgeCV on train is 747.4410131599824\n"
     ]
    }
   ],
   "source": [
    "###################  岭回归／L2正则  ###################\n",
    "from sklearn.linear_model import RidgeCV\n",
    "\n",
    "# 设置超参数(正则参数)范围\n",
    "alphas = [0.01, 0.1, 1, 10, 100]\n",
    "# 生成RidgeCV实例\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)\n",
    "# 训练模型\n",
    "ridge.fit(X_train, y_train)\n",
    "# 预测\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    "# 使用【RMSE】评价模型在测试集和训练集上的性能\n",
    "print('The RMSE score of RidgeCV on test is', np.sqrt(mean_squared_error(y_test, y_test_pred_ridge)))\n",
    "print('The RMSE score of RidgeCV on train is', np.sqrt(mean_squared_error(y_train, y_train_pred_ridge)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The RMSE score of LassoCV on test is 1428.6588490895804\n",
      "The RMSE score of LassoCV on train is 1439.5361015009635\n"
     ]
    }
   ],
   "source": [
    "###################  Lasso／L1正则  ###################\n",
    "from sklearn.linear_model import LassoCV\n",
    "# 使用默认超参数搜索\n",
    "lasso = LassoCV()\n",
    "# 训练\n",
    "lasso.fit(X_train, y_train.values.ravel())\n",
    "# 测试\n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "# 使用【RMSE】评价模型在测试集和训练集上的性能\n",
    "print('The RMSE score of LassoCV on test is', np.sqrt(mean_squared_error(y_test, y_test_pred_lasso)))\n",
    "print('The RMSE score of LassoCV on train is', np.sqrt(mean_squared_error(y_train, y_train_pred_lasso)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>yr</td>\n",
       "      <td>[4550.708896660368]</td>\n",
       "      <td>[1504.623924128292]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>temp</td>\n",
       "      <td>[2654.792827053679]</td>\n",
       "      <td>[1778.493413538692]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth_9</td>\n",
       "      <td>[1287.9923600829734]</td>\n",
       "      <td>[678.3529307999597]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>atemp</td>\n",
       "      <td>[995.2937777539719]</td>\n",
       "      <td>[1546.3740337700867]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth_10</td>\n",
       "      <td>[929.8876485595974]</td>\n",
       "      <td>[84.87302004857884]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>[914.4109085449156]</td>\n",
       "      <td>[914.843091785845]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>season_4</td>\n",
       "      <td>[830.5795181835542]</td>\n",
       "      <td>[767.2053171804819]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>mnth_12</td>\n",
       "      <td>[586.2964051248265]</td>\n",
       "      <td>[-824.9285955290934]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth_8</td>\n",
       "      <td>[517.8030382681848]</td>\n",
       "      <td>[205.68600901568757]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>[409.58907935902243]</td>\n",
       "      <td>[388.86521490064297]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth_11</td>\n",
       "      <td>[407.1388034475102]</td>\n",
       "      <td>[-725.8285290476842]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>weekday_6</td>\n",
       "      <td>[238.41731182264104]</td>\n",
       "      <td>[227.51574041336562]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>workingday</td>\n",
       "      <td>[216.95654890754108]</td>\n",
       "      <td>[212.55871019439826]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth_6</td>\n",
       "      <td>[189.7972164585105]</td>\n",
       "      <td>[369.6631544418742]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>season_2</td>\n",
       "      <td>[85.14188881840792]</td>\n",
       "      <td>[110.99861389424723]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weekday_5</td>\n",
       "      <td>[77.51626320592048]</td>\n",
       "      <td>[81.39654973380311]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday_3</td>\n",
       "      <td>[66.46478675151441]</td>\n",
       "      <td>[59.85793277146786]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday_4</td>\n",
       "      <td>[43.65054603321886]</td>\n",
       "      <td>[62.795850371407596]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth_5</td>\n",
       "      <td>[5.009200114222085]</td>\n",
       "      <td>[387.71362769248094]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>[-6.919060283513033]</td>\n",
       "      <td>[1.4171574763022363]</td>\n",
       "      <td>5.456671</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday_2</td>\n",
       "      <td>[-31.943212383671575]</td>\n",
       "      <td>[-34.14008002871242]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>season_3</td>\n",
       "      <td>[-130.80716202639073]</td>\n",
       "      <td>[-91.38827520385439]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>weekday_0</td>\n",
       "      <td>[-191.61244555993915]</td>\n",
       "      <td>[-191.85150952264985]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>weekday_1</td>\n",
       "      <td>[-202.49324986968833]</td>\n",
       "      <td>[-205.57448373868556]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth_7</td>\n",
       "      <td>[-203.13758243830083]</td>\n",
       "      <td>[-242.54858152421093]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>holiday</td>\n",
       "      <td>[-263.76141517023797]</td>\n",
       "      <td>[-248.222941085117]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth_4</td>\n",
       "      <td>[-485.7142387363415]</td>\n",
       "      <td>[106.30651277828429]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>mnth_3</td>\n",
       "      <td>[-541.4399172496096]</td>\n",
       "      <td>[298.5500495645838]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season_1</td>\n",
       "      <td>[-784.9142449755683]</td>\n",
       "      <td>[-786.8156558708756]</td>\n",
       "      <td>-341.190505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-1174.8457900947863]</td>\n",
       "      <td>[-1087.7731976474147]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>mnth_2</td>\n",
       "      <td>[-1207.1118918481309]</td>\n",
       "      <td>[-143.12524857819005]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>hum</td>\n",
       "      <td>[-1298.592137982125]</td>\n",
       "      <td>[-1142.3972760584988]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>[-1323.9999879039378]</td>\n",
       "      <td>[-1303.708306686488]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>mnth_1</td>\n",
       "      <td>[-1486.5210417835176]</td>\n",
       "      <td>[-194.7143496622698]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         columns                coef_lr             coef_ridge  coef_lasso\n",
       "33            yr    [4550.708896660368]    [1504.623924128292]    0.000000\n",
       "27          temp    [2654.792827053679]    [1778.493413538692]    0.000000\n",
       "13        mnth_9   [1287.9923600829734]    [678.3529307999597]    0.000000\n",
       "28         atemp    [995.2937777539719]   [1546.3740337700867]    0.000000\n",
       "14       mnth_10    [929.8876485595974]    [84.87302004857884]    0.000000\n",
       "17  weathersit_1    [914.4109085449156]     [914.843091785845]    0.000000\n",
       "4       season_4    [830.5795181835542]    [767.2053171804819]   -0.000000\n",
       "16       mnth_12    [586.2964051248265]   [-824.9285955290934]   -0.000000\n",
       "12        mnth_8    [517.8030382681848]   [205.68600901568757]    0.000000\n",
       "18  weathersit_2   [409.58907935902243]   [388.86521490064297]   -0.000000\n",
       "15       mnth_11    [407.1388034475102]   [-725.8285290476842]   -0.000000\n",
       "26     weekday_6   [238.41731182264104]   [227.51574041336562]   -0.000000\n",
       "32    workingday   [216.95654890754108]   [212.55871019439826]    0.000000\n",
       "10        mnth_6    [189.7972164585105]    [369.6631544418742]    0.000000\n",
       "2       season_2    [85.14188881840792]   [110.99861389424723]    0.000000\n",
       "25     weekday_5    [77.51626320592048]    [81.39654973380311]    0.000000\n",
       "23     weekday_3    [66.46478675151441]    [59.85793277146786]    0.000000\n",
       "24     weekday_4    [43.65054603321886]   [62.795850371407596]    0.000000\n",
       "9         mnth_5    [5.009200114222085]   [387.71362769248094]    0.000000\n",
       "0        instant   [-6.919060283513033]   [1.4171574763022363]    5.456671\n",
       "22     weekday_2  [-31.943212383671575]   [-34.14008002871242]    0.000000\n",
       "3       season_3  [-130.80716202639073]   [-91.38827520385439]    0.000000\n",
       "20     weekday_0  [-191.61244555993915]  [-191.85150952264985]   -0.000000\n",
       "21     weekday_1  [-202.49324986968833]  [-205.57448373868556]   -0.000000\n",
       "11        mnth_7  [-203.13758243830083]  [-242.54858152421093]    0.000000\n",
       "31       holiday  [-263.76141517023797]    [-248.222941085117]   -0.000000\n",
       "8         mnth_4   [-485.7142387363415]   [106.30651277828429]    0.000000\n",
       "7         mnth_3   [-541.4399172496096]    [298.5500495645838]    0.000000\n",
       "1       season_1   [-784.9142449755683]   [-786.8156558708756] -341.190505\n",
       "30     windspeed  [-1174.8457900947863]  [-1087.7731976474147]   -0.000000\n",
       "6         mnth_2  [-1207.1118918481309]  [-143.12524857819005]   -0.000000\n",
       "29           hum   [-1298.592137982125]  [-1142.3972760584988]   -0.000000\n",
       "19  weathersit_3  [-1323.9999879039378]   [-1303.708306686488]   -0.000000\n",
       "5         mnth_1  [-1486.5210417835176]   [-194.7143496622698]   -0.000000"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对比最小二乘线性回归、岭回归和Lasso各特征的系数\n",
    "fs = pd.DataFrame({'columns':list(feature_names), \n",
    "                   'coef_lr':list((lr.coef_.T)), \n",
    "                   'coef_ridge':list((ridge.coef_.T)), \n",
    "                   'coef_lasso':list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'], ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "从上面的对比表格可以看到：\n",
    "    最小二乘线性回归和岭回归都发生过拟合，回归系数（绝对值/平方）很大，即输入𝑥的很小变化\n",
    "可能带来输出𝑦较大的变化，函数变化剧烈。从回归系数分析来看，Lasso线性回归模型的性能在三者\n",
    "中是比较好的。\n",
    "\"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
